package com.le.core.web.service.impl;

import com.le.business.after.domin.UserIdentity;
import com.le.business.after.service.UserIdentityService;
import com.le.common.enums.UserStatus;
import com.le.common.utils.StringUtils;
import com.le.core.login.domin.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserIdentityService leUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserIdentity userIdentity = new UserIdentity();
        userIdentity.setUserName(username);
        List<UserIdentity> userIdentities = leUserService.selectTList(userIdentity);

        if (StringUtils.isEmpty(userIdentities)||userIdentities.size()!=1)
        {
            throw new RuntimeException("登录用户：" + username + " 异常");
        }

        UserDetails loginUser = createLoginUser(userIdentities.get(0));

        return loginUser;
    }

    public UserDetails createLoginUser(UserIdentity user)
    {
        LoginUser loginUser = new LoginUser();
        loginUser.setLeUser(user);
        loginUser.setIdentityId(user.getIdentityId());
        return loginUser;
    }
}
